﻿using System;
using System.Windows;
using System.Windows.Threading;
using Rabbit.Utils;
using System.Diagnostics;

namespace MicroBlogClient
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        static System.Windows.Forms.NotifyIcon  notifyIcon = new System.Windows.Forms.NotifyIcon();
        static DispatcherTimer tmrTwinkleSysTrayIcon = new DispatcherTimer();
        static System.Drawing.Icon appIcon = 
            new System.Drawing.Icon(
            Application.GetResourceStream(
                new Uri("/Resources/logo.ico", 
                    UriKind.Relative)).Stream);

        static System.Drawing.Icon appTransIcon = new System.Drawing.Icon(
            Application.GetResourceStream(
                new Uri("/Resources/transIcon.ico",
                    UriKind.Relative)).Stream);

        private void Application_Startup(object sender, StartupEventArgs e)
        { 
            tmrTwinkleSysTrayIcon.Interval = TimeSpan.FromSeconds(0.5);
            tmrTwinkleSysTrayIcon.Tick += new EventHandler(tmrBlendSysTrayIcon_Tick);
            Resources.MergedDictionaries.Clear();
            Resources.MergedDictionaries.Add(Rabbit.Themes.ThemeManager.GetTheme("Win7"));
            
            this.initNotifyIcon();
        }

        void tmrBlendSysTrayIcon_Tick(object sender, EventArgs e)
        {
            if (notifyIcon.Icon == appIcon)
            {
                notifyIcon.Icon = appTransIcon;
            }
            else
            {
                notifyIcon.Icon = appIcon;
            }
        }

        public static void TwinkleSysTrayIcon(string message = "有新的消息，点击查看")
        {
            notifyIcon.Text = message;
            tmrTwinkleSysTrayIcon.IsEnabled = true;
        }

        private void initNotifyIcon()
        {
            notifyIcon.Icon = appIcon;
            notifyIcon.Text = Application.Current.Resources["AppTitle"].ToString();
            notifyIcon.Visible = true;
            notifyIcon.Click += new EventHandler(notifyIcon_Click);

            System.Windows.Forms.ContextMenu cmMenu = new System.Windows.Forms.ContextMenu();
            System.Windows.Forms.MenuItem miClose = new System.Windows.Forms.MenuItem();
            miClose.Text = "退出";
            miClose.Click += new EventHandler((a, b) => { this.Shutdown(0); });

            System.Windows.Forms.MenuItem miShowLog = new System.Windows.Forms.MenuItem();
            miShowLog.Text = "查看日志";
            miShowLog.Click += new EventHandler((a, b) =>
            {
                Rabbit.Utils.LogHelper.OpenLogFolder();
            });

            System.Windows.Forms.MenuItem miAbout = new System.Windows.Forms.MenuItem();
            miAbout.Text = "关于";
            miAbout.Click += new EventHandler((a, b) =>
            {
                About about = new About();
                about.ShowDialog();
            });

            System.Windows.Forms.MenuItem miAuthorWebsite = new System.Windows.Forms.MenuItem();
            miAuthorWebsite.Text = "作者微博";
            miAuthorWebsite.Click += new EventHandler((o, e) =>
            {
                System.Diagnostics.Process.Start(Application.Current.Resources["AuthorWebSite"].ToString());
            });

            cmMenu.MenuItems.Add(0, miAuthorWebsite);
            cmMenu.MenuItems.Add(1, miAbout);
            cmMenu.MenuItems.Add(2, miShowLog);
            cmMenu.MenuItems.Add(3, miClose);
            notifyIcon.ContextMenu = cmMenu;
        }


        private void Application_Exit(object sender, ExitEventArgs e)
        {
            //remove notifyIcon
            notifyIcon.Dispose();
        }
        void notifyIcon_Click(object sender, EventArgs e)
        {
            StopTwinkleSysTrayIcon();
            this.MainWindow.WindowState = WindowState.Normal;
            this.MainWindow.Show();
            this.MainWindow.Activate();
            //if (this.MainWindow.Top <= 0)
            //{
            //    this.MainWindow.Top = 0;
            //}
            //if (this.MainWindow.WindowState == System.Windows.WindowState.Minimized)
            //{
            //    this.MainWindow.WindowState = System.Windows.WindowState.Normal;
            //}
        }

        public static void StopTwinkleSysTrayIcon()
        {
            notifyIcon.Text = Application.Current.Resources["AppTitle"].ToString();
            notifyIcon.Icon = appIcon;
            tmrTwinkleSysTrayIcon.IsEnabled = false;
        }

        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            //MessageBox.Show("发生未知的异常,您可以将异常信息反馈给开发者：" + Environment.NewLine + e.Exception.Message);
            LogHelper.LogError(e.Exception);
            e.Handled = true;
        }
    }
}
